import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

@Slf4j
public class httpclient {
    /**
     * GET---无参测试
     *
     * @date 2018年7月13日 下午4:18:50
     */
    @Test
    public void doGetTestOne() {
        //实例化
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //创建htp请求
        HttpGet httpGet = new HttpGet("https://www.baidu.com/");
        try {
            //请求地址
            log.info("httpGet.getURI()："+httpGet.getURI());
            //请求方式
            log.info("httpGet.getMethod()："+httpGet.getMethod());
            log.info("httpGet.getAllHeaders()："+httpGet.getAllHeaders());
            log.info("httpGet.getConfig()："+httpGet.getConfig());
            log.info("httpGet.getHeaders()："+httpGet.getHeaders(""));
            log.info("httpGet.getFirstHeader()："+httpGet.getFirstHeader(""));
            log.info("httpGet.getLastHeader()："+httpGet.getLastHeader(""));
            //http协议版本
            log.info("httpGet.getProtocolVersion()："+httpGet.getProtocolVersion());
            log.info("httpGet.getRequestLine()："+httpGet.getRequestLine());

            //执行get请求
            CloseableHttpResponse execute = httpClient.execute(httpGet);
            log.info("响应状态："+execute.getStatusLine());
            HttpEntity entity = execute.getEntity();
            log.info("内容长度："+entity.getContentLength());
            log.info("媒体格式类型"+entity.getContentType());
            log.info("响应内容："+ EntityUtils.toString(entity,"UTF-8"));
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                httpClient.close();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    @Test
    public void postMethods(){
        String authorization= "eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6ImQwMWQ4MzA3LWJhZGUtNDc1ZS1iNzhmLTdkNjQwMWYzMzYyNSJ9.zk34K-gLzmG--nTLKYsmnq7NszfVMTaB2NFSi7g3r0xxZmj7VfTADqpdi4CEAFGAgrZiqO2LCvQiY4vP6HcJug";
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost("http://localhost:8080/system/questioncategory/upload");
        //文件
        FileBody fileBody = new FileBody(new File("C:/Users/yangshaui/Pictures/Saved Pictures/aHR0cDovL2ltZy5ibG9nLmNzZG4ubmV0LzIwMTcxMTI4MTUyNDA5NjIy.png"));
        //文本内容
        StringBody stringBody = new StringBody("A binary file of some kind", ContentType.TEXT_PLAIN);
        HttpEntity build = MultipartEntityBuilder.create().addPart("file", fileBody).addPart("comment", stringBody).build();

        //参数集合
        List params = new ArrayList<>();
        params.add(new BasicNameValuePair("Authorization",authorization));
        UrlEncodedFormEntity urlEncodedFormEntity;
        try {
            //参数
            urlEncodedFormEntity = new UrlEncodedFormEntity(params,"UTF-8");
            httpPost.setEntity(build);
            //请求头
            httpPost.setHeader("Authorization",authorization);
            try {
                //发送Post请求
                CloseableHttpResponse execute = httpClient.execute(httpPost);
                log.info("响应状态："+execute.getStatusLine());
                HttpEntity entity = execute.getEntity();
                log.info("------"+entity.toString(),"UTF-8");
                log.info("111111"+entity.getContent());

            } catch (IOException e) {
//                log.info(ExceptionUtils.getStackTrace(e));
                e.printStackTrace();
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }finally {
            try {
                httpClient.close();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
}
